** Plot similarity measures by chain across channels 
** JHL  

** Similarity measures 
** For each pair of stores i and j in chain c, average across products and quarters 
** 1. Weekly correlation in deviation from quarterly mean

*************************************
** Set up workspace
*************************************
version 15.0
clear all
set more off

cd "${path_home}"
adopath + ../programs

** log using "${path_log}/a07_similarity_top1", text replace

*************************************
** Start work here
*************************************
timer on 1 


*************************************
** [1] Chains and rigidity 
*************************************

use "${path_dta}/nielsen/stores_2006_chain_chars", clear 

gen parent_code_1 = parent_code 
merge m:1 parent_code_1 channel_code using "${path_dta}/nielsen/top1_cor_parent_x_state_by_parent", keep(master match) nogen 
** Too few observations 
drop if parent_code_1 == 4999

** Store type and rigidity 
ta channel_code, su(rigid6)	
ta channel_code, su(pct_d_cor_wi_parent)

** Paper: Plot distribution of rigidity measure by channel code with cutoff threshold, store frequencies 
tw (hist pct_d_cor_wi_parent if channel_code=="D", fcolor(orange%30) lcolor(orange) freq width(0.01) legend(lab(1 "Drug"))) ///
(hist pct_d_cor_wi_parent if channel_code=="F", fcolor(green%60) lcolor(green) freq width(0.01) legend(lab(2 "Grocery"))) ///
(hist pct_d_cor_wi_parent if channel_code=="M", fcolor(none) lcolor(blue) freq width(0.01) legend(lab(3 "Merchandise"))), ///
scheme(plotplainblind) xtitle("Differences in within vs. across state average correlations") legend(pos(6) col(3))
translate @Graph "${path_out}/graphs/rigidity_dist_DFM.pdf", name("Graph")

** Black and white 
tw (hist pct_d_cor_wi_parent if channel_code=="D", fcolor(gray%30) lcolor(gray%30) freq width(0.01) legend(lab(1 "Drug"))) ///
(hist pct_d_cor_wi_parent if channel_code=="F", fcolor(gray%100) lcolor(gray%100) freq width(0.01) legend(lab(2 "Grocery"))) ///
(hist pct_d_cor_wi_parent if channel_code=="M", fcolor(none) lcolor(black) freq width(0.01) legend(lab(3 "Merchandise"))), ///
scheme(plotplainblind) xtitle("Differences in within vs. across state average correlations") legend(pos(6) col(3))
translate @Graph "${path_out}/graphs/rigidity_dist_DFM_bw.pdf", name("Graph")

** Paper: Plot distribution of pstate 
tw (hist pstate if channel_code=="D", fcolor(orange%30) lcolor(orange) freq width(1) legend(lab(1 "Drug"))) ///
(hist pstate if channel_code=="F", fcolor(green%60) lcolor(green) freq width(1) legend(lab(2 "Grocery"))) ///
(hist pstate if channel_code=="M", fcolor(none) lcolor(blue) freq width(1) legend(lab(3 "Merchandise"))), ///
scheme(plotplainblind) xtitle("Number of states per chain for all stores") legend(pos(6) col(3))
translate @Graph "${path_out}/graphs/pstate_dist_DFM.pdf", name("Graph")

** Black and white 
tw (hist pstate if channel_code=="D", fcolor(gray%30) lcolor(gray%30) freq width(1) legend(lab(1 "Drug"))) ///
(hist pstate if channel_code=="F", fcolor(gray%100) lcolor(gray%100) freq width(1) legend(lab(2 "Grocery"))) ///
(hist pstate if channel_code=="M", fcolor(none) lcolor(black) freq width(1) legend(lab(3 "Merchandise"))), ///
scheme(plotplainblind) xtitle("Number of states per chain for all stores") legend(pos(6) col(3))
translate @Graph "${path_out}/graphs/pstate_dist_DFM_bw.pdf", name("Graph")


*************************************
** Close workspace
*************************************
timer off 1
timer list 1
** log close
